package com.fingard.dsp.bank.directbank.ynrcc01;

import com.fingard.dsp.bank.directbank.bankInterface.IBalance;
import com.fingard.dsp.bank.reqrespobj.balance.Req7505HisBalQry;
import com.fingard.dsp.bank.reqrespobj.balance.Req7506BalQry;
import com.fingard.dsp.bank.reqrespobj.balance.RetAts7505HisBalQry;
import com.fingard.dsp.bank.reqrespobj.balance.RetAts7506BalQry;
import com.fingard.dsp.bank.reqrespobj.balance.detail.RetAtsBalDetail;
import com.fingard.xml.XmlTextReader;
import com.fingard.xml.XmlTextWriter;

/**
 * Created by F on 2018/5/10.
 */
public class YNRCC01Balance extends YNRCC01Base implements IBalance {
    /**
     * 今日余额
     * @param p_req7506
     * @return
     * @throws Exception
     */
    @Override
    public RetAts7506BalQry d_7506_BalQry(Req7506BalQry p_req7506) throws Exception {
        RetAts7506BalQry retAts7506BalQry = new RetAts7506BalQry(p_req7506.reqHeader);
        retAts7506BalQry.writeStart();
        try {
            while (p_req7506.readRecord()) {
                RetAtsBalDetail detail = new RetAtsBalDetail();
                //非公有参数
                XmlTextWriter xmlTextWriter = new XmlTextWriter();
                YNRCC01Header header = new YNRCC01Header();
                header.transCode ="B2EActBalQry";
                header.erpJnlNo=p_req7506.reqHeader.transSeqID;
                wirteCommenHeader(xmlTextWriter,header);
                xmlTextWriter.writeStartElement("Body");
                xmlTextWriter.writeStartElement("Map");
                xmlTextWriter.writeElementString("AcNo",p_req7506.readedDetail.actNum);//帐号
                xmlTextWriter.writeElementString("Currency",convertCurToBank(p_req7506.readedDetail.actCur));//币种
                xmlTextWriter.writeEndElement();
                xmlTextWriter.writeEndDocument();
                //请求银行
                String[] bankResult = sendToBank(xmlTextWriter.toXmlString());

                //处理响应报文
                if (bankResult[0].length() == 0) {
                    StringBuilder tmpSb = new StringBuilder();
                    XmlTextReader tmpBkReader = loadBankRespXmlReader(bankResult[1], tmpSb);if (tmpBkReader != null) {
                        String respCode = tmpBkReader.getFirstText("/Message/Head/ReturnCode");
                        String respInfo = tmpBkReader.getFirstText("/Message/Head/ReturnMsg");
                        if ("000000".equals(respCode)) {//交易成功
                            if (tmpBkReader.read("/Message/Body/Map")) {
                                detail.respCode = "0";
                                detail.respInfo = respInfo;
                                detail.actCur = p_req7506.readedDetail.actCur;
                                detail.actNum = p_req7506.readedDetail.actNum;
                                detail.availBal = tmpBkReader.readedXElement.getSingleTextNotNull("AvailBal");//可用余额
                                detail.currBal = tmpBkReader.readedXElement.getSingleTextNotNull("Balance");//余额
                            }
                        }else{
                            detail.respCode = "-1";
                            detail.respInfo = respInfo;
                        }
                    }else{
                        detail.respCode = "-1";
                        detail.respInfo = tmpSb.toString();
                    }
                } else {
                    detail = new RetAtsBalDetail();
                    detail.respCode = "-1";
                    detail.respInfo = bankResult[1];
                }
                retAts7506BalQry.writeRetDetail(detail, p_req7506.isBatch);
            }
        } catch (Exception e) {
            retAts7506BalQry.writeRespCode("-1");
            retAts7506BalQry.writeRespInfo(e.getMessage());
            e.printStackTrace();
        }
        retAts7506BalQry.writeEnd();
        return retAts7506BalQry;
    }

    @Override
    public RetAts7505HisBalQry d_7505_BalQry(Req7505HisBalQry p_req7505) throws Exception {
        return null;
    }
}
